/
start.ts
91 lines (78 loc) · 2.11 KB
/
start.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Command, flags } from "@oclif/command";
import execa = require("execa");
import { green, red, yellow, cyan } from "colors";
import { exists, existsSync, writeFileSync } from "fs";
export default class Start extends Command {
static description = "start application";
static flags = {
help: flags.help({ char: "h" }),
// env
env: flags.string({
char: "e",
description: "application environnement",
options: ["local", "production", "development"],
exclusive: ["local", "production", "development"],
default: (ctx) => {
// local
if (ctx.flags.local) {
return "local";
}
// development
if (ctx.flags.development) {
return "development";
}
// production
if (ctx.flags.production) {
return "production";
}
return undefined;
},
}),
// port
port: flags.string({
description: "application port",
}),
// local environnement
local: flags.boolean({
char: "l",
description: "local environnement",
default: false,
exclusive: ["development", "production"],
}),
// development environnement
development: flags.boolean({
char: "d",
description: "development environnement",
default: false,
exclusive: ["local", "production"],
}),
// production environnement
production: flags.boolean({
char: "p",
description: "production environnement",
default: false,
exclusive: ["local", "development"],
}),
};
static args = [];
async run() {
const { args, flags } = this.parse(Start);
if (flags.env && !existsSync(`.env.${flags.env}`)) {
writeFileSync(`.env.${flags.env}`, '');
}
// starting the app
await execa("npm", ["run", flags.env ? `start:${flags.env}` : "start"], {
env: {
PORT: flags.port,
},
stdio: [0, 1, 2],
})
.then((result) => {
console.log(result.stdout);
console.log(cyan(result.stderr));
})
.catch((reason) => {
this.log(cyan(reason));
});
}
}